package com.drinkian.authentication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.drinkian.authentication.entity.bo.SecurityUser;
import com.drinkian.authentication.entity.po.*;
import com.drinkian.authentication.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) {
        // 1.1 查询用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("status", 2);
        // === user ===
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw new RuntimeException("未查询到该用户名！");
        }

        List<String> roleList = new LinkedList<>();
        List<String> permissionList = new LinkedList<>();

        // 2 查询用户角色关联表
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", user.getId());
        // === user_role ===
        List<UserRole> userRoleList = userRoleMapper.selectList(userRoleQueryWrapper);
        for (UserRole userRole : userRoleList) {
            // 3 查询相关角色
            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq("id", userRole.getRoleId());
            // === role ===
            Role role = roleMapper.selectOne(roleQueryWrapper);
            roleList.add(role.getName());

            // 4 查询角色权限关联表
            QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
            rolePermissionQueryWrapper.eq("role_id", role.getId());
            // === role_permission ===
            List<RolePermission> rolePermissionList = rolePermissionMapper.selectList(rolePermissionQueryWrapper);
            for (RolePermission rolePermission : rolePermissionList) {
                // 查询角色相关权限
                QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
                permissionQueryWrapper.eq("id", rolePermission.getPermissionId());
                // === permission ===
                Permission permission = permissionMapper.selectOne(permissionQueryWrapper);
                permissionList.add(permission.getName());
            }
        }

        // 3.1 封装成UserDetails接口实现类进行返回
        return new SecurityUser(user, roleList, permissionList);
    }
}
